package com.hisicom.scqxspxcjc.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.hisicom.scqxspxcjc.base.bean.Result;
import com.hisicom.scqxspxcjc.base.tool.JwtTokenUtil;
import com.hisicom.scqxspxcjc.services.SystemCheckServiceBus;
import io.jsonwebtoken.ExpiredJwtException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("sceneCheck")
public class LoginController {
    @Autowired
    SystemCheckServiceBus systemCheckServiceBus;

    @PostMapping(value = "/login")
    @ResponseBody
    public Result appAuthentication(String account, String password, HttpServletResponse response) {
        try {
            //1.调用微服务查询用户信息
            Boolean login = systemCheckServiceBus.login(account, password);
            if (!login) {
                return Result.error("用户名或密码错误。");
            } else {
                Map<String, Object> claims = new HashMap<>();
                claims.put("account", account);
                //生成token
                String jwtToken = JwtTokenUtil.generateToken(account, 9000000, claims);
                response.setHeader("Authorization", jwtToken);
                return Result.success(claims);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //app用户验证
    @PostMapping(value = "/appVerify")
    @ResponseBody
    public Result appVerifyUser(String Authorization, HttpServletResponse response) {
        //验证
        Result re = new Result();
        if (StrUtil.isNotBlank(Authorization)) {
            String salt = null;
            try {
                JwtTokenUtil.isExpiration(Authorization);
                salt = JwtTokenUtil.parseToken(Authorization, "_salt");
                if (salt != null && salt != "") {
                    Map<String, Object> claims1 = JwtTokenUtil.getClaims(Authorization);
                    Date expiration = JwtTokenUtil.getTokenBody(Authorization).getExpiration();
                    long nowTime = System.currentTimeMillis();
                    long expirationTime = expiration.getTime();
                    // 结束时间-开始时间 = 分钟
                    long minute = (expirationTime - nowTime) / (60 * 1000);
                    String jwtToken = null;
                    if (minute <= 60) {
                        jwtToken = JwtTokenUtil.generateToken(claims1.get("account") + "", 9000000, claims1);
                    } else {
                        jwtToken = Authorization;
                    }
                    response.setHeader("Authorization", jwtToken);
                    re.setSuccess(true);
                    claims1.put("Authorization", jwtToken);
                    re.setObj(claims1);
                } else {
                    re.setSuccess(false);
                    re.setCode(401);
                    re.setMsg("认证失败");
                }
            } catch (ExpiredJwtException e) {
                if (e.getMessage().contains("Allowed clock skew")) {
                    re.setSuccess(false);
                    re.setCode(401);
                    re.setMsg("认证过期");
                } else {
                    re.setSuccess(false);
                    re.setCode(401);
                    re.setMsg("认证失败");
                }
            } catch (Exception e) {
                e.printStackTrace();
                re.setSuccess(false);
                re.setCode(401);
                re.setMsg("认证失败");
            }
        } else {
            re.setSuccess(false);
            re.setCode(401);
            re.setMsg("认证失败");
        }
        return re;
    }
}
